R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

plot(pressure)

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

# Horizontal Bar Plot for 
# Ozone concentration in air 
barplot(airquality$Ozone, 
        main = 'Ozone Concenteration in air', 
        xlab = 'ozone levels', horiz = TRUE) 

S.No Parameter Description 1. H A vector or matrix which contains numeric values used in the bar chart. 2. xlab A label for the x-axis. 3. ylab A label for the y-axis. 4. main A title of the bar chart. 5. names.arg A vector of names that appear under each bar. 6. col It is used to give colors to the bars in the graph.

# Vertical Bar Plot for 
# Ozone concentration in air 
barplot(airquality$Ozone, main = 'Ozone Concenteration in air', 
        xlab = 'ozone levels', col ='blue', horiz = FALSE) 

# Creating the data for Bar chart  
H<- c(12,35,54,3,41)  
# Giving the chart file a name  
#png(file = "bar_chart.png")  
# Plotting the bar chart   
barplot(H)  

# Saving the file  
#dev.off()  

Labels, Title & Colors

# Creating the data for Bar chart  
H <- c(12,35,54,3,41)  
M<- c("Feb","Mar","Apr","May","Jun")  
  
# Giving the chart file a name  
#png(file = "bar_properties.png")  
  
# Plotting the bar chart   
barplot(H,names.arg=M,xlab="Month",ylab="Revenue",col="Green",  
        main="Revenue Bar chart",border="red")  

# Saving the file  
#dev.off()  

Group Bar Chart & Stacked Bar Chart

library(RColorBrewer)  
months <- c("Jan","Feb","Mar","Apr","May")  
regions <- c("West","North","South")  
# Creating the matrix of the values.  
Values <- matrix(c(21,32,33,14,95,46,67,78,39,11,22,23,94,15,16), nrow = 3, ncol = 5, byrow = TRUE)  
# Giving the chart file a name  
png(file = "bca_4_b.png")  
# Creating the bar chart  
barplot(Values, main = "Total Revenue", names.arg = months, xlab = "Month", ylab = "Revenue", col =c("cadetblue3","deeppink2","goldenrod1"))  
# Adding the legend to the chart  
legend("topleft", regions, cex = 1.3, fill = c("cadetblue3","deeppink2","goldenrod1"))  
  
# Saving the file  
dev.off()  
## quartz_off_screen 
##                 2

boxplot(x, data, notch, varwidth, names, main)

S.No Parameter Description 1. x It is a vector or a formula. 2. data It is the data frame. 3. notch It is a logical value set as true to draw a notch. 4. varwidth It is also a logical value set as true to draw the width of the box same as the sample size. 5. names It is the group of labels that will be printed under each boxplot. 6. main It is used to give a title to the graph.

# Giving a name to the chart file.  
#png(file = "boxplot.png")  
# Plotting the chart.  
boxplot(mpg ~ cyl, data = mtcars, xlab = "Quantity of Cylinders",  
        ylab = "Miles Per Gallon", main = "R Boxplot Example")  

# Save the file.  
#dev.off()  

Boxplot using notch

# Giving a name to our chart.  
# png(file = "boxplot_using_notch.png")  
# Plotting the chart.  
boxplot(mpg ~ cyl, data = mtcars,   
        xlab = "Quantity of Cylinders",  
        ylab = "Miles Per Gallon",   
        main = "Boxplot Example",  
        notch = TRUE,   
        varwidth = TRUE,   
        col = c("green","yellow","red"),  
        names = c("High","Medium","Low")  
)  
## Warning in (function (z, notch = FALSE, width = NULL, varwidth = FALSE, : some
## notches went outside hinges ('box'): maybe set notch=FALSE

# Saving the file.  
# dev.off()  
# Histogram for Maximum Daily Temperature 
data(airquality) 

hist(airquality$Temp, main ="La Guardia Airport's\ 
Maximum Temperature(Daily)", 
    xlab ="Temperature(Fahrenheit)", 
    xlim = c(50, 125), col ="yellow", 
    freq = TRUE) 

barplot(iris$Petal.Length) #Creating simple Bar Graph

barplot(iris$Sepal.Length,col  = brewer.pal(3,"Set1"))

barplot(table(iris$Species,iris$Sepal.Length),col  = brewer.pal(3,"Set1")) #Stacked Plot

library(RColorBrewer)
data(VADeaths)
par(mfrow=c(2,3))
hist(VADeaths,breaks=10, col=brewer.pal(3,"Set3"),main="Set3 3 colors")
hist(VADeaths,breaks=3 ,col=brewer.pal(3,"Set2"),main="Set2 3 colors")
hist(VADeaths,breaks=7, col=brewer.pal(3,"Set1"),main="Set1 3 colors")
hist(VADeaths,,breaks= 2, col=brewer.pal(8,"Set3"),main="Set3 8 colors")
hist(VADeaths,col=brewer.pal(8,"Greys"),main="Greys 8 colors")
hist(VADeaths,col=brewer.pal(8,"Greens"),main="Greens 8 colors")

# Box plot for average wind speed 
data(airquality) 

boxplot(airquality$Wind, main = "Average wind speed\ 
at La Guardia Airport", 
        xlab = "Miles per hour", ylab = "Wind", 
        col = "orange", border = "brown", 
        horizontal = TRUE, notch = TRUE) 

# Multiple Box plots, each representing 
# an Air Quality Parameter 
boxplot(airquality[, 0:4], 
        main ='Box Plots for Air Quality Parameters') 

boxplot(iris$Petal.Length~iris$Species) #Creating Box Plot between two variable

data(iris)
par(mfrow=c(2,2))
boxplot(iris$Sepal.Length,col="red")
boxplot(iris$Sepal.Length~iris$Species,col="red")
boxplot(iris$Sepal.Length~iris$Species,col=heat.colors(3))
boxplot(iris$Sepal.Length~iris$Species,col=topo.colors(3))

# Scatter plot for Ozone Concentration per month 
data(airquality) 

plot(airquality$Ozone, airquality$Month, 
    main ="Scatterplot Example", 
    xlab ="Ozone Concentration in parts per billion", 
    ylab =" Month of observation ", pch = 19) 

# Set seed for reproducibility 
# set.seed(110) 

# Create example data 
data <- matrix(rnorm(50, 0, 5), nrow = 5, ncol = 5) 
## Warning in matrix(rnorm(50, 0, 5), nrow = 5, ncol = 5): data length differs
## from size of matrix: [50 != 5 x 5]
# Column names 
colnames(data) <- paste0("col", 1:5) 
rownames(data) <- paste0("row", 1:5) 

# Draw a heatmap 
heatmap(data)        

install.packages("maps", repos = "http://cran.us.r-project.org")
## 
## The downloaded binary packages are in
##  /var/folders/hw/yd8h01ln5ss03m6g_j88q8780000gn/T//RtmpDSsvxz/downloaded_packages
# Read dataset and convert it into 
# Dataframe 
#data <- read.csv("worldcities.csv") 
data <- read.csv(file = "data/data.csv") 
## Warning in read.table(file = file, header = header, sep = sep, quote = quote, :
## incomplete final line found by readTableHeader on 'data/data.csv'
df <- data.frame(data) 

# Load the required libraries 
library(maps) 
map(database = "world") 
    
# marking points on map 
points(x = df$Lat[1:500], y = df$Lon[1:500], col = "Red") 

# Create the data for the chart.
v <- c(17, 25, 38, 13, 41)

# Plot the bar chart.
plot(v, type = "o")

plot(AirPassengers,type="l")  #Simple Line Plot

# Create the data for the chart.
v <- c(17, 25, 38, 13, 41)

# Plot the bar chart.
plot(v, type = "o", col = "green",
    xlab = "Month", ylab = "Article Written",
    main = "Article Written chart")

# Create the data for the chart.
v <- c(17, 25, 38, 13, 41)
t <- c(22, 19, 36, 19, 23)
m <- c(25, 14, 16, 34, 29)

# Plot the bar chart.
plot(v, type = "o", col = "red", xlab = "Month", ylab = "Article Written ",
    main = "Article Written chart")
lines(t, type = "o", col = "blue")
lines(m, type = "o", col = "green")

# Create the data for the chart
A <- c(17, 32, 8, 53, 1)

# Plot the bar chart
barplot(A, xlab = "X-axis", ylab = "Y-axis", main ="Bar-Chart")

# Create the data for the chart
A <- c(17, 32, 8, 53, 1)

# Plot the bar chart
barplot(A, horiz = TRUE, xlab = "X-axis",
        ylab = "Y-axis", main ="Horizontal Bar Chart"
    )

# Create the data for the chart
A <- c(17, 2, 8, 13, 1, 22)
B <- c("Jan", "feb", "Mar", "Apr", "May", "Jun")

# Plot the bar chart
barplot(A, names.arg = B, xlab ="Month",
        ylab ="Articles", col ="green",
        main ="GeeksforGeeks-Article chart")

# Create the data for the chart
A <- c(17, 2, 8, 13, 1, 22)
B <- c("Jan", "Feb", "Mar", "Apr", "May", "Jun")

# Plot the bar chart with text features
barplot(A, names.arg = B, xlab = "Month",
        ylab = "Articles", col = "steelblue",
        main = "GeeksforGeeks - Article Chart",
        cex.main = 1.5, cex.lab = 1.2, cex.axis = 1.1)

# Add data labels on top of each bar
text(
x = barplot(A, names.arg = B, col = "steelblue", ylim = c(0, max(A) * 1.2)),
y = A + 1, labels = A, pos = 3, cex = 1.2, col = "black"
)

colors = c("green", "orange", "brown")
months <- c("Mar", "Apr", "May", "Jun", "Jul")
regions <- c("East", "West", "North")

# Create the matrix of the values.
Values <- matrix(c(2, 9, 3, 11, 9, 4, 8, 7, 3, 12, 5, 2, 8, 10, 11),
                nrow = 3, ncol = 5, byrow = TRUE)

# Create the bar chart
barplot(Values, main = "Total Revenue", names.arg = months,
                        xlab = "Month", ylab = "Revenue",
                        col = colors, beside = TRUE)

# Add the legend to the chart
legend("topleft", regions, cex = 0.7, fill = colors)

colors = c("green", "orange", "brown")
months <- c("Mar", "Apr", "May", "Jun", "Jul")
regions <- c("East", "West", "North")

# Create the matrix of the values.
Values <- matrix(c(2, 9, 3, 11, 9, 4, 8, 7, 3, 12, 5, 2, 8, 10, 11),nrow = 3, ncol = 5, byrow = TRUE)

# Create the bar chart
barplot(Values, main = "Total Revenue", names.arg = months,
        xlab = "Month", ylab = "Revenue", col = colors)

plot(iris$Sepal.Length, iris$Sepal.Width, cex = 1, pch = 3,
     xlab ="x", ylab ="y",
     col ="black")
# Add the legend to the chart
legend("topleft", regions, cex = 0.7, fill = colors)

plot(iris,col=brewer.pal(3,"Set1"))

pie(table(iris$Species))

pie(X, Labels, Radius, Main, Col, Clockwise)
X is a vector that contains the numeric values used in the pie chart. Labels are used to give the description to the slices. Radius describes the radius of the pie chart. Main describes the title of the chart. Col defines the color palette. Clockwise is a logical value that indicates the clockwise or anti-clockwise direction in which slices are drawn.

# Creating data for the graph.  
x <- c(20, 65, 15, 50)  
labels <- c("India", "America", "Shri Lanka", "Nepal")  
# Giving the chart file a name.  
png(file = "Country.jpg")  
# Plotting the chart.  
pie(x,labels)  
# Saving the file.  
dev.off() 
## quartz_off_screen 
##                 2
# Creating data for the graph.  
x <- c(20, 65, 15, 50)  
labels <- c("India", "America", "Shri Lanka", "Nepal")  
# Giving the chart file a name.  
png(file = "title_color.jpg")  
# Plotting the chart.  
pie(x,labels,main="Country Pie chart",col=rainbow(length(x)))  
# Saving the file.  
dev.off()  
## quartz_off_screen 
##                 2
# Creating data for the graph.  
x <- c(20, 65, 15, 50)  
labels <- c("India", "America", "Shri Lanka", "Nepal")  
pie_percent<- round(100*x/sum(x), 1)  
# Giving the chart file a name.  
png(file = "per_pie.jpg")  
# Plotting the chart.  
pie(x, labels = pie_percent, main = "Country Pie Chart",col = rainbow(length(x)))  
legend("topright", c("India", "America", "Shri Lanka", "Nepal"), cex = 0.8,  
fill = rainbow(length(x)))  
#Saving the file.  
dev.off()  
## quartz_off_screen 
##                 2
# Getting the library. 
install.packages("plotrix", repos = "http://cran.us.r-project.org")
## 
## The downloaded binary packages are in
##  /var/folders/hw/yd8h01ln5ss03m6g_j88q8780000gn/T//RtmpDSsvxz/downloaded_packages
library(plotrix)  
# Creating data for the graph.  
x <- c(20, 65, 15, 50,45)  
labels <- c("India", "America", "Shri Lanka", "Nepal","Bhutan")  
# Give the chart file a name.  
png(file = "3d_pie_chart1.jpg")  
# Plot the chart.  
pie3D(x,labelslabels = labels,explode = 0.1, main = "Country Pie Chart")  
## Warning in plot.window(...): "labelslabels" is not a graphical parameter
## Warning in plot.xy(xy, type, ...): "labelslabels" is not a graphical parameter
## Warning in title(...): "labelslabels" is not a graphical parameter
# Save the file.  
dev.off()  
## quartz_off_screen 
##                 2
# Getting the library.  
library(plotrix)  
# Creating data for the graph.  
x <- c(20, 65, 15, 50,45)  
labels <- c("India", "America", "Shri Lanka", "Nepal","Bhutan")  
pie_percent<- round(100*x/sum(x), 1)  
# Giving the chart file a name.  
png(file = "three_D_pie.jpg")  
# Plotting the chart.  
pie3D(x, labels = pie_percent, main = "Country Pie Chart",col = rainbow(length(x)))  
legend("topright", c("India", "America", "Shri Lanka", "Nepal","Bhutan"), cex = 0.8,  
fill = rainbow(length(x)))  
#Saving the file.  
dev.off()  
## quartz_off_screen 
##                 2
install.packages("hexbin", repos ="http://cran.us.r-project.org")
## 
## The downloaded binary packages are in
##  /var/folders/hw/yd8h01ln5ss03m6g_j88q8780000gn/T//RtmpDSsvxz/downloaded_packages
library(hexbin)
library(ggplot2)
data("diamonds")
a<-hexbin(diamonds$price,diamonds$carat,xbins=40)
library(RColorBrewer)
plot(a)

install.packages("RColorBrewer", repos = "http://cran.us.r-project.org")
## 
## The downloaded binary packages are in
##  /var/folders/hw/yd8h01ln5ss03m6g_j88q8780000gn/T//RtmpDSsvxz/downloaded_packages
library(RColorBrewer)
library(ggplot2)
rf <- colorRampPalette(rev(brewer.pal(40,'Set3')))
## Warning in brewer.pal(40, "Set3"): n too large, allowed maximum for palette Set3 is 12
## Returning the palette you asked for with that many colors
data(diamonds)
hexbinplot(diamonds$price~diamonds$carat, data=diamonds, colramp=rf)

data(HairEyeColor)
mosaicplot(HairEyeColor)

heatmap(as.matrix(mtcars))



```r
install.packages("magrittr", repos = "http://cran.us.r-project.org")
## 
## The downloaded binary packages are in
##  /var/folders/hw/yd8h01ln5ss03m6g_j88q8780000gn/T//RtmpDSsvxz/downloaded_packages
library(magrittr)
install.packages("leaflet", repos = "http://cran.us.r-project.org")
## 
## The downloaded binary packages are in
##  /var/folders/hw/yd8h01ln5ss03m6g_j88q8780000gn/T//RtmpDSsvxz/downloaded_packages
library(leaflet)
m <- leaflet() %>%
addTiles() %>%  # Add default OpenStreetMap map tiles
addMarkers(lng=77.2310, lat=28.6560, popup="The delicious food of chandni chowk")
m  # Print the map

ggplot

# Print the top 5 records of the dataset
head(mtcars)
# Installing the package
install.packages("dplyr", repos = "http://cran.us.r-project.org")
## 
## The downloaded binary packages are in
##  /var/folders/hw/yd8h01ln5ss03m6g_j88q8780000gn/T//RtmpDSsvxz/downloaded_packages
# Loading package
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Summary of dataset in package
summary(mtcars)
##       mpg             cyl             disp             hp       
##  Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
##  1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
##  Median :19.20   Median :6.000   Median :196.3   Median :123.0  
##  Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
##  3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
##  Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
##       drat             wt             qsec             vs        
##  Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
##  1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
##  Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
##  Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
##  3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
##  Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
##        am              gear            carb      
##  Min.   :0.0000   Min.   :3.000   Min.   :1.000  
##  1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
##  Median :0.0000   Median :4.000   Median :2.000  
##  Mean   :0.4062   Mean   :3.688   Mean   :2.812  
##  3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
##  Max.   :1.0000   Max.   :5.000   Max.   :8.000
install.packages("ggplot2", repos = "http://cran.us.r-project.org")
## 
##   There is a binary version available but the source version is later:
##         binary source needs_compilation
## ggplot2  3.4.4  3.5.0             FALSE
## installing the source package 'ggplot2'
library(ggplot2)
#library(dplyr)

ggplot(data = mtcars) +
labs(title = "MTCars Data Plot",
     y="This is y axis", x="this is x-axis")

# Aesthetic Layer
ggplot(data = mtcars, aes(x = hp, y = mpg, col = disp))+
labs(title = "MTCars Data Plot")

# Geometric layer
ggplot(data = mtcars, aes(x = hp, y = mpg, col = disp)) +
geom_point() +
labs(title = "Miles per Gallon vs Horsepower",
    x = "Horsepower",
    y = "Miles per Gallon")

# Adding size
ggplot(data = mtcars, aes(x = hp, y = mpg, size = disp)) +
geom_point() +
labs(title = "Miles per Gallon vs Horsepower",
    x = "Horsepower",
    y = "Miles per Gallon")

# Adding size
ggplot(data = iris, aes(x = Sepal.Length, y = Sepal.Width, col = Species)) +
geom_point() +
labs(title = "Iris Flower Sepal Lenght vs Sepal Width",
    x = "Sepal Length",
    y = "Sepal Width")

cor(iris$Sepal.Length, iris$Sepal.Width)
## [1] -0.1175698
# Adding shape and color
ggplot(data = mtcars, aes(x = hp, y = mpg, col = factor(cyl),
    shape = factor(am))) +geom_point() +
labs(title = "Miles per Gallon vs Horsepower",
    x = "Horsepower",
    y = "Miles per Gallon")

# Histogram plot
ggplot(data = mtcars, aes(x = hp)) +
geom_histogram(binwidth = 5) +
labs(title = "Histogram of Horsepower",
    x = "Horsepower",
    y = "Count")

# Histogram plot
ggplot(data = iris, aes(x = Sepal.Length)) +
geom_histogram() +
labs(title = "Iris Flower Sepal Length",
    x = "Sepal Length",
    y = "Measure")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# Facet Layer
# Separate rows according to transmission type
p <- ggplot(data = mtcars, aes(x = hp, y = mpg, shape = factor(cyl))) + geom_point()

p

p + facet_grid(am ~ .) +
labs(title = "Miles per Gallon vs Horsepower",
    x = "Horsepower",
    y = "Miles per Gallon")

# Separate columns according to cylinders
p <- ggplot(data = mtcars, aes(x = hp, y = mpg, shape = factor(cyl))) + geom_point()

p + facet_grid(. ~ cyl) +
labs(title = "Miles per Gallon vs Horsepower",
    x = "Horsepower",
    y = "Miles per Gallon")

ggplot(data = mtcars, aes(x = hp, y = mpg)) +
geom_point() +
stat_smooth(method = lm, col = "red") +
labs(title = "Miles per Gallon vs Horsepower")
## `geom_smooth()` using formula = 'y ~ x'

ggplot(data = mtcars, aes(x = wt, y = mpg)) +
geom_point() +
stat_smooth(method = lm, col = "red") +
scale_y_continuous("Miles per Gallon", limits = c(6, 35), expand = c(0, 0)) +
scale_x_continuous("Weight", limits = c(0, 8), expand = c(0, 0)) +
coord_equal() +
labs(title = "Miles per Gallon vs Weight",
    x = "Weight",
    y = "Miles per Gallon")
## `geom_smooth()` using formula = 'y ~ x'

# Add coord_cartesian() to proper zoom in
ggplot(data = mtcars, aes(x = wt, y = hp, col = am)) +
                        geom_point() + geom_smooth() +
                        coord_cartesian(xlim = c(3, 6))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: The following aesthetics were dropped during statistical transformation: colour
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

ggplot(data = mtcars, aes(x = hp, y = mpg)) +
geom_point() +
facet_grid(. ~ cyl) +
theme(plot.background = element_rect(fill = "blue", colour = "gray")) +
labs(title = "Miles per Gallon vs Horsepower")

ggplot(data = mtcars, aes(x = hp, y = mpg)) +
        geom_point() + facet_grid(am ~ cyl) + 
        theme_gray()+
labs(title = "Miles per Gallon vs Horsepower")

# Install and load required packages
install.packages("ggplot2", repos = "http://cran.us.r-project.org")
## 
##   There is a binary version available but the source version is later:
##         binary source needs_compilation
## ggplot2  3.4.4  3.5.0             FALSE
## installing the source package 'ggplot2'
library(ggplot2)

# Create a 2D density contour plot for the mtcars dataset
ggplot(mtcars, aes(x = wt, y = mpg)) +
stat_density_2d(aes(fill = ..level..), geom = "polygon", color = "white") +
scale_fill_viridis_c() + 
labs(title = "2D Density Contour Plot of mtcars Dataset",
    x = "Weight (wt)",
    y = "Miles per Gallon (mpg)",
    fill = "Density") +
theme_minimal()
## Warning: The dot-dot notation (`..level..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(level)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

library(ggplot2)
install.packages("gridExtra", repos = "http://cran.us.r-project.org")
## 
## The downloaded binary packages are in
##  /var/folders/hw/yd8h01ln5ss03m6g_j88q8780000gn/T//RtmpDSsvxz/downloaded_packages
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
# Selecting specific columns from mtcars dataset
selected_cols <- c("mpg", "disp", "hp", "drat")
selected_data <- mtcars[, selected_cols]

# Create histograms for individual variables
hist_plot_mpg <- ggplot(selected_data, aes(x = mpg)) +
geom_histogram(binwidth = 2, fill = "blue", color = "white") +
labs(title = "Histogram: Miles per Gallon", x = "Miles per Gallon", y = "Frequency")

hist_plot_disp <- ggplot(selected_data, aes(x = disp)) +
geom_histogram(binwidth = 50, fill = "red", color = "white") +
labs(title = "Histogram: Displacement", x = "Displacement", y = "Frequency")

hist_plot_hp <- ggplot(selected_data, aes(x = hp)) +
geom_histogram(binwidth = 20, fill = "green", color = "white") +
labs(title = "Histogram: Horsepower", x = "Horsepower", y = "Frequency")

hist_plot_drat <- ggplot(selected_data, aes(x = drat)) +
geom_histogram(binwidth = 0.5, fill = "orange", color = "white") +
labs(title = "Histogram: Drat", x = "Drat", y = "Frequency")

# Arrange the plots in a grid
grid.arrange(hist_plot_mpg, hist_plot_disp, hist_plot_hp, hist_plot_drat,
            ncol = 2)

# Create a plot
plot <- ggplot(data = mtcars, aes(x = hp, y = mpg)) +
geom_point() +
labs(title = "Miles per Gallon vs Horsepower")

# Save the plot as an image file (e.g., PNG)
ggsave("plot.png", plot)
## Saving 7 x 5 in image
# Save the plot as a PDF file
ggsave("plot.pdf", plot)
## Saving 7 x 5 in image
# Extract the plot as a variable for further use
extracted_plot <- plot
plot

install.packages("tidyverse", repos = "http://cran.us.r-project.org")
## 
## The downloaded binary packages are in
##  /var/folders/hw/yd8h01ln5ss03m6g_j88q8780000gn/T//RtmpDSsvxz/downloaded_packages
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.3     ✔ tibble    3.2.1
## ✔ purrr     1.0.2     ✔ tidyr     1.3.1
## ✔ readr     2.1.5     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ gridExtra::combine() masks dplyr::combine()
## ✖ tidyr::extract()     masks magrittr::extract()
## ✖ dplyr::filter()      masks stats::filter()
## ✖ dplyr::lag()         masks stats::lag()
## ✖ purrr::map()         masks maps::map()
## ✖ purrr::set_names()   masks magrittr::set_names()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy))

- The plot shows a negative relationship between engine size (displ) and fuel efficiency (hwy). In other words, cars with big engines use more fuel. - Does this confirm or refute your hypothesis about fuel efficiency and engine size?

ggplot() creates a coordinate system that you can add layers to. complete your graph by adding one or more layers to ggplot(). geom_point() adds a layer of points to the plot, which creates a scatterplot. ggplot2 comes with many geom functions that each add a different type of layer to a plot. Each geom function in ggplot2 takes a mapping argument. how variables in your dataset are mapped to visual properties. The mapping argument is always paired with aes(), and the x and y arguments of aes() specify which variables to map to the x and y axes. ggplot2 looks for the mapped variables in the data argument, in this case, mpg.

ggplot template

ggplot(data = ) + (mapping = aes())